//
//  DataCheck.m
//  New_Runway
//
//  Created by mack on 2018/3/15.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "DataCheck.h"
#import "RegexKitLite.h"

@implementation DataCheck

+ (BOOL)valiMobile:(NSString *)mobile
{
    mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (mobile.length != 11)
    {
        return NO;
    }else{
        NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";

        NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
        NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
        NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";

        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
        NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
        NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
        
        if (([regextestmobile evaluateWithObject:mobile] == YES)
            || ([regextestcm evaluateWithObject:mobile] == YES)
            || ([regextestct evaluateWithObject:mobile] == YES)
            || ([regextestcu evaluateWithObject:mobile] == YES))
        {
            return YES;
        }
        else
        {
            return NO;
            
        }
    }
}

+ (BOOL)isStringPureNumber:(NSString *)theString
{
    NSScanner *scanner = [NSScanner scannerWithString:theString];
    int val;
            return [scanner scanInt:&val] && [scanner isAtEnd];
}
+ (BOOL)isFistCharEnglishLetter:(NSString *)theText
{
    BOOL is = NO;
    if (theText && [theText isKindOfClass:[NSString class]] && theText.length > 0) {
        int ival = [theText characterAtIndex:0];
        if ( (ival >= 65 && ival <= 90) || (ival >= 97 && ival <= 122) ) {
            is = YES;
        }
    }
    return is;
}
+ (BOOL)isFistCharChinese:(NSString *)theText
{
    BOOL is = NO;
    if (theText && [theText isKindOfClass:[NSString class]] && theText.length > 0) {
        NSRange range = NSMakeRange(0, 1);
        NSString *subString = [theText substringWithRange:range];
        const char *firstChar = [subString UTF8String];
        if (3 == strlen(firstChar)) {
            is = YES;
        }
    }
    return is;
}
//是否含有表情
+ (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    return returnValue;
}

+ (BOOL)stringContainsVisibleCharacter:(NSString *)string
{
    BOOL contrain = NO;
    
    // 去除空格
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    // 去除换行符
    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    contrain = (string.length > 0);
    
    return contrain;
}

+ (BOOL)isLegalHKDemailCharacterRegister:(NSString *)text
{
    if ([text isMatchedByRegex:@"[0-9a-zA-Z]"])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)isLegalHKDemailCharacterRegisterVin:(NSString *)text
{
    BOOL result = false;
    if ([text length] == 17){        
        NSString * regex = @"^[0-9A-Z]{17}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:text];
        if (result == true) {
            BOOL result1 = false;
            NSString * regex1 = @".*[QIOqoi].*";
            NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
            result1 = [pred1 evaluateWithObject:text];
            if (result1 == YES) {
                result = NO;
            }else{
                result = YES;
            }
        }
        
    }
    return result;
}

+(NSString  *)string:(NSString *)string replaceStr:(NSString *)replaceStr{
    
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"Oo" options:0 error:nil];
    string  = [regularExpression stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, string.length) withTemplate:replaceStr];
    return string;
}


+(BOOL)JudgeTheillegalCharacter:(NSString *)text{
    //提示 标签不能输入特殊字符
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    if (![emailTest evaluateWithObject:text]) {
        return YES;
    }
    return NO;
}

+ (BOOL)isLegalHXAccountCharacter:(NSString *)text
{
    if ([text isMatchedByRegex:@"[0-9.]"])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+(BOOL)StringIsNullOrEmpty:(NSString *)str
{
    return (str == nil || [str isKindOfClass:[NSNull class]] || str.length == 0);
}
@end
